package main

import (
	"database/sql"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
	"net/http"
	"strconv"
	"strings"
	"twcdb/middleware"
)
var db_attend *sql.DB

func sqlOpen_attend() {
	var err error
	db_attend, err = sql.Open("postgres", "port=5432 user=postgres password=123 dbname=postgres sslmode=disable")
	checkErr_attend(err)
	println("连接成功")
}
func main() {


	r := gin.Default()
	r.Use(middleware.CORSMiddleware())
	r.POST("/attend", func(ctx *gin.Context) {

		dat :=ctx.PostForm("dat")
		dwj :=ctx.PostForm("dwj")
		dml :=ctx.PostForm("dml")
		di :=ctx.PostForm("di")
		dab :=ctx.PostForm("dab")
		dl :=ctx.PostForm("dl")
		dot :=ctx.PostForm("dot")
		injuryrate :=ctx.PostForm("injuryrate")
		//fmt.Println("这里")
		ctx.Request.ParseForm()


		for k:= range ctx.Request.PostForm{

			fmt.Printf("k:%v\n", k)
			s0 := k[2:4]

			s1 :=k[14: 19]
			s2 :=k[30: 37]
			s3 :=k[46: 56]
			if len(k)>100{
				//s4 :=k[3:6] //dab

				s5 :=k[9:10]  //dab_data
				s6 :=k[19:20] // dat
				s7 :=k[28:29] //di
				s8 :=k[37:38] //dl
				s9 :=k[47:48] //dml
				s10 :=k[57:58] //dot
				s11 :=k[67:68] //dwj
				s12 :=k[77:87] //eno
				s13 :=k[103:104] //injuryrate

				s14 :=k[115:122] //month
				if s0=="mo"{
					s33 :=k[18:25]
					fmt.Printf("testing")
					s35 :=k[35:45]
					s36 :=k[54:55]
					s37 :=k[64:65]
					s38 :=k[74:75]
					s39 :=k[83:84]
					s40 :=k[93:94]
					s41 :=k[102:103]
					s43:=k[112:113]
					s44 :=k[129:130]
					//fmt.Printf(s33,s35,s36,s37,s38,s39,s40,s41,s43,s44)
					fmt.Printf(s33)
					fmt.Printf(s35)
					fmt.Printf(s36)
					fmt.Printf(s37)
					fmt.Printf(s38)
					fmt.Printf(s39)
					fmt.Printf(s40)
					fmt.Printf(s41)
					fmt.Printf(s43)
					fmt.Printf(s44)
					sqlInsert_attend(s33,s35,s36,s37,s38,s39,s40,s41,s43,s44)

				}
				if len(k)>126{
					//s15 :=k[126:130]
					//fmt.Printf(s14)
					sqlUpdate_attend(s12,s14,s5,s6,s7,s8,s9,s10,s11,s13)
				}

				//fmt.Printf(s4+" "+s5+" "+s6+" "+s7+" "+s8+" "+s9+" "+s10+" "+s11+" "+s12+" "+s13+" "+s14)
			}

			if s1=="selec" {
				dat,dwj,dml,di,dab,dl,dot,injuryrate=sqlSelect_attend(s2,s3)
				ctx.JSON(http.StatusOK,gin.H{"eno":s3,"month":s2,"dat":dat,"dwj":dwj,"dml":dml,"di":di,"dab":dab,"dl":dl,"dot":dot,"injuryrate":injuryrate})
			}

			if s1=="delet" {
				sqlDelete_attend(s3,s2)
				//dat,dwj,dml,di,dab,dl,dot,injuryrate=sqlSelect_attend(s2,s3)
				//ctx.JSON(http.StatusOK,gin.H{"eno":s3,"month":s2,"dat":dat,"dwj":dwj,"dml":dml,"di":di,"dab":dab,"dl":dl,"dot":dot,"injuryrate":injuryrate})
			}
		}
		//for j:=0;j<len(a);j++{
		//	fmt.Println(a[j])
		//}


		//switch a[0] {
		//case "insert":
		//	if len(eno) ==0{
		//		ctx.JSON(http.StatusUnprocessableEntity,gin.H{"mag":"编号为空"})
		//	}
		//	sqlInsert_attend(month,eno,dat,dwj,dml,di,dab,dl,dot,injuryrate)
		//case "selec":
		//	dat,dwj,dml,di,dab,dl,dot,injuryrate=sqlSelect_attend(a[1],a[2])
		//	ctx.JSON(http.StatusOK,gin.H{"eno":eno,"month":month,"dat":dat,"dwj":dwj,"dml":dml,"di":di,"dab":dab,"dl":dl,"dot":dot,"injuryrate":injuryrate})
		//
		//
		//case "update":
		//	sqlUpdate_attend(eno,month,dl)
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"msg":"修改成功"})
		//case "delete":
		//	sqlDelete_attend(eno,month)
		//	ctx.JSON(http.StatusUnprocessableEntity,gin.H{"msg":"成功删除"})
		//}
		//ctx.JSON(200, gin.H{
		//	"message": "通讯完成",
		//})
		//sqlClose_attend()
	})
	panic(r.Run(":8086") )// listen and serve on 0.0.0.0:8181

}
func sqlInsert_attend(month,eno,dat,dwj,dml,di,dab,dl,dot,injuryrate string) {
	//插入数据
	sqlOpen_attend()
	stmt, err := db_attend.Prepare("INSERT INTO attend(month,eno,dat,dwj,dml,di,dab,dl,dot,injuryrate) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) ")
	checkErr_attend(err)

	res, err := stmt.Exec(month,eno,dat,dwj,dml,di,dab,dl,dot,injuryrate)
	//这里的三个参数就是对应上面的$1,$2,$3了

	checkErr_attend(err)

	affect, err := res.RowsAffected()
	checkErr_attend(err)

	fmt.Println("rows affect:", affect)
	fmt.Println("success")
}
func sqlSelect_attend(months,enos string) (dat,dwj,dml,di,dab,dl,dot,injuryrate string){
	//查询数据
	sqlOpen_attend()
	rows, err := db_attend.Query("SELECT * FROM attend ")
	checkErr_attend(err)

	//s1 :=strconv.Append
	println("-----------")
	for rows.Next() {

		var eno string
		var month string
		var dat int
		var dwj int
		var dml int
		var di int
		var dab int
		var dl int
		var dot int
		var injuryrate int
		rows.Scan(&month, &eno,&dat, &dwj, &dml, &di, &dab, &dl, &dot, &injuryrate)
		if true{
			//fmt.Println(eno)
			//fmt.Println(month)
			println("-----------")
			//fmt.Println(enos)
			//fmt.Println("这里执行了1")
			//if true{
				//fmt.Println("这里执行了2")
				 //rows.Scan(&month, &eno,&dat, &dwj, &dml, &di, &dab, &dl, &dot, &injuryrate)
			//}
			fmt.Println(eno)
			fmt.Println(months)
			if eno==months{
				fmt.Println("这里执行了1")
				if month==enos{
					//fmt.Println("这里执行了2")
					fmt.Println( months,enos,dat,dwj,dml,di,dab,dl,dot,injuryrate)
					str1 := strconv.Itoa(dat)
					str2 := strconv.Itoa(dwj)
					str3 := strconv.Itoa(dml)
					str4 := strconv.Itoa(di)
					str5 := strconv.Itoa(dab)
					str6 := strconv.Itoa(dl)
					str7 := strconv.Itoa(dot)
					str8 := strconv.Itoa(injuryrate)
					return str1,str2,str3,str4,str5,str6,str7,str8
				}
			}

		}
	}
		//checkErr_attend(err)
		////fmt.Println(eno,ename)
		//str1 := strconv.Itoa(dat)
		//str2 := strconv.Itoa(dwj)
		//str3 := strconv.Itoa(dml)
		//str4 := strconv.Itoa(di)
		//str5 := strconv.Itoa(dab)
		//str6 := strconv.Itoa(dl)
		//str7 := strconv.Itoa(dot)
		//str8 := strconv.Itoa(injuryrate)
		//fmt.Println( months,enos,str1,str2,str3,str4,str5,str6,str7,str8)
		//return str1,str2,str3,str4,str5,str6,str7,str8


	return "0","0","0","0","0","0","0","0"
}
func sqlUpdate_attend(eno, month ,dab,dat,di,dl,dml,dot,dwj,injuryrate string) {
	//更新数据
	sqlOpen_attend()
	stmt, err := db_attend.Prepare("update attend set dat=$1,dwj=$2,dml=$3,di=$4,dab=$5,dl=$6,dot=$7,injuryrate=$8  where eno=$9 and month=$10")
	checkErr_attend(err)
	//fmt.Println(eno, month ,dab,dat,di,dl,dml,dot,dwj,injuryrate)
	num1, err := strconv.Atoi(dab)
	num2, err := strconv.Atoi(dat)
	num3, err := strconv.Atoi(di)
	num4, err := strconv.Atoi(dl)
	num5, err := strconv.Atoi(dml)
	num6, err := strconv.Atoi(dot)
	num7, err := strconv.Atoi(dwj)
	num8, err := strconv.Atoi(injuryrate)
    //fmt.Println(num1,num2,num3,num4,num5,num6,num7,num8,eno,month)
	res, err := stmt.Exec(num1,num2,num3,num4,num5,num6,num7,num8,eno,month)
	//res, err := stmt.Exec("ficow", 1)
	checkErr_attend(err)

	affect, err := res.RowsAffected()
	checkErr_attend(err)

	fmt.Println("rows affect:", affect)
}
func sqlDelete_attend(eno,month string) {
	//删除数据
	sqlOpen_attend()
	stmt, err := db_attend.Prepare("delete from attend where eno=$1 and month=$2")
	checkErr_attend(err)
	res, err := stmt.Exec(eno,month)

	checkErr_attend(err)

	affect, err := res.RowsAffected()
	checkErr_attend(err)

	fmt.Println("rows affect:", affect)
}
func sqlClose_attend() {
	db_attend.Close()
}
func checkErr_attend(err error) {
	if err != nil {
		panic(err)
	}
}
func splitString(r rune) bool {
	return r == ':' || r == '-'
}
func splitStringChar(s string) []string {
	a := strings.FieldsFunc(s, splitString)
	return a
}